#pragma once
#include <iostream>
#include <string>
#include "Protocol.hpp"


//提供计算服务
class SeverCal
{
public:
    SeverCal(){}
    ~SeverCal(){}

public:
    Response Calculate(const Request& req)
    {
        Response resp(0,0);
        switch(req.op)
        {
            case '+':
                resp.result = req.x + req.y;
                break;
            case '-':
                resp.result = req.x - req.y;
                break;
            case '*':
                resp.result = req.x * req.y;
                break;
            case '/':
                if(req.y == 0)resp.code = -1;
                else resp.result = req.x / req.y;
                break;     
            default:
                resp.code = -2;
                break;       
        }

        return resp;
    }

    std::string Getback(std::string& package)
    {   
        //拿到有效载荷-->反序列化
        std::string content;
        bool success = Decode(package, &content);
        if(!success) return "";

        Request req;
        success =  req.Deserialize(content);
        if(!success) return "";

        //根据拿到的数据-->处理结果-->构建响应(序列化 + 添加报头)
        Response resp = Calculate(req);
        std::string back;
        resp.Serialize(&back);
        back = Encode(back);

        return back;
    }

    
};



